package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.FocusNewsService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *要闻动态管理
 */
@RestController
@RequestMapping("/bk")
public class FocusNewsController {

    private static final Logger logger = LogManager.getLogger(FocusNewsController.class);
    
    @Resource
    private FocusNewsService focusNewsService;

    @Resource
    private ArticleSearchService articleSearchService;


    @LoginRequired
    @PostMapping("/addFocusNews")
    @ApiOperation(value = "新增要闻")
    public JsonResult addFocusNews(
            @ApiParam(value = "要闻标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "要闻副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "是否推荐 0 ： 不推荐 1：推荐") @RequestParam(value = "isRecommend",required = false) String isRecommend,
            @ApiParam(value = "是否置顶 0 ： 不置顶 1：置顶") @RequestParam(value = "isTop",required = false) String isTop,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "fTypeId",required = false) String fTypeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("fTypeId", fTypeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("isRecommend", isRecommend);
            pageData.put("imgUrl", imgUrl);
            pageData.put("img", imgUrl);
            pageData.put("isTop", isTop);
            pageData.put("description", description);

            focusNewsService.addFocusNews(pageData);

            try {
                pageData.put("tableName","t_focus_news");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateFocusNews")
    @ApiOperation(value = "更新要闻")
    public JsonResult updateFocusNews(
            @ApiParam(value = "要闻ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "要闻标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "要闻副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "是否推荐 0 ： 不推荐 1：推荐") @RequestParam(value = "isRecommend",required = false) String isRecommend,
            @ApiParam(value = "是否置顶 0 ： 不置顶 1：置顶") @RequestParam(value = "isTop",required = false) String isTop,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "fTypeId",required = false) String fTypeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("content", content);
            pageData.put("fTypeId", fTypeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("isRecommend", isRecommend);
            pageData.put("imgUrl", imgUrl);
            pageData.put("img", imgUrl);
            pageData.put("isTop", isTop);
            pageData.put("description", description);
            focusNewsService.updateFocusNews(pageData);

            try {
                pageData.put("tableName","t_focus_news");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteFocusNews")
    @ApiOperation(value = "删除要闻")
    public JsonResult deleteFocusNews(
            @ApiParam(value = "要闻ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            focusNewsService.deleteFocusNews(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_focus_news");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableFocusNews")
    @ApiOperation(value = "禁用或启用要闻")
    public JsonResult disableOrEnableFocusNews(
            @ApiParam(value = "要闻ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            focusNewsService.disableOrEnableFocusNews(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listFocusNews")
    @ApiOperation(value = "获取要闻列表")
    public JsonResult listFocusNews(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(focusNewsService.listFocusNews(pageData));
            result.setResultNum(focusNewsService.listFocusNewsCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addFocusNewsType")
    @ApiOperation(value = "新增要闻类型")
    public JsonResult addFocusNewsType(
            @ApiParam(value = "要闻类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "要闻类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);

            focusNewsService.addFocusNewsType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateFocusNewsType")
    @ApiOperation(value = "更新要闻类型")
    public JsonResult updateFocusNewsType(
            @ApiParam(value = "要闻类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "要闻类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "要闻类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("remark", remark);

            focusNewsService.updateFocusNewsType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteFocusNewsType")
    @ApiOperation(value = "删除要闻类型")
    public JsonResult deleteFocusNewsType(
            @ApiParam(value = "要闻类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            focusNewsService.deleteFocusNewsType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableFocusNewsType")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableFocusNewsType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            focusNewsService.disableOrEnableFocusNewsType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listFocusNewsType")
    @ApiOperation(value = "获取要闻类型列表")
    public JsonResult listFocusNewsType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(focusNewsService.listFocusNewsType(pageData));
            result.setResultNum(focusNewsService.listFocusNewsTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listFocusNewsTypeAll")
    @ApiOperation(value = "获取要闻类型列表所有")
    public JsonResult listFocusNewsTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(focusNewsService.listFocusNewsTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
